﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Net;
using WebApiV2.Interface.Log;

namespace WebApiV2.Utils.Filters
{
    public class GlobalExceptionFilter : IExceptionFilter
    {
        private readonly ILog _log;

        public GlobalExceptionFilter(ILog log)
        {
            _log = log;
        }

        public void OnException(ExceptionContext context)
        {


            _log.Error(context.Exception);

            _log.Error(context.Exception);

            if (context.ExceptionHandled == false)
            {
                context.Result = new ContentResult
                {
                    Content = context.Exception.Message,//异常抛出
                };
            }
            context.ExceptionHandled = true;
        }


    }
}
